﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using CrystalDecisions.CrystalReports.Engine;
using System.Reflection;
using System.IO;


namespace Plugin
{
    public class Config
    {
        /// <summary>
        /// 服务名称
        /// </summary>
        private string _ServiceName = "长虹装箱运输单打印";

        public string ServiceName
        {
            get
            {
                return _ServiceName;
            }
        }

        /// <summary>
        /// 插件版本
        /// </summary>
        private string _DLLVersion = "1.0";

        public string DLLVersion
        {
            get
            {
                return _DLLVersion;
            }
        }

        /// <summary>
        /// 运行间隔，默认60秒执行一次
        /// </summary>
        private int _RunInterval = 60;

        public int RunInterval
        {
            get
            {
                return _RunInterval;
            }
        }

        /// <summary>
        /// 显示设置窗体
        /// </summary>
        public void ShowConfig()
        {
            frmConfig newFrmConfig = new frmConfig();
            newFrmConfig.Show();
        }
    }

    public class Service
    {
        private SqlTools sqt;

        public Service()
        {
            //初始化SQL查询对象
            sqt = new SqlTools(GeneralTools.GetConnectionString("TPAMain"));
        }

        public void Start()
        {
            //检查当前是否有504所销货单
            string strTmpSelectString = "select xha001 销货单号,htb027 合同号,xhb003 规格型号,xhb008 数量 from orxha_table ,orxhb_table left join htb_table on htb001=xhb033 and htb002 =xhb034 where xha001=xhb001 and xha013='Y' and xha007 ='SCCH' and xha001 not in (select 销货单号 from zxysd_ch) order by xhb001,xhb002";
            DataTable tmpdt = sqt.GetData(strTmpSelectString);
            if (tmpdt != null && tmpdt.Rows.Count > 0)
            {
                try
                {
                    List<DataSet> lstDs = new List<DataSet>();
                    List<string> lstXhdh = new List<string>();
                    StringBuilder tmpsb = new StringBuilder();
                    //送货单位主管
                    GeneralTools.GetPrivateProfileString("Config", "Manager", string.Empty, tmpsb, 256, System.Windows.Forms.Application.StartupPath + "\\Plugin\\Print_Zxysd_ch_Config.ini");
                    string strManager = tmpsb.ToString();
                    //送货单位库房保管
                    GeneralTools.GetPrivateProfileString("Config", "StorageManager", string.Empty, tmpsb, 256, System.Windows.Forms.Application.StartupPath + "\\Plugin\\Print_Zxysd_ch_Config.ini");
                    string strStorageManager = tmpsb.ToString();
                    //送货单位经办
                    GeneralTools.GetPrivateProfileString("Config", "Operator", string.Empty, tmpsb, 256, System.Windows.Forms.Application.StartupPath + "\\Plugin\\Print_Zxysd_ch_Config.ini");
                    string strOperator = tmpsb.ToString();
                    //生成报表数据
                    string strTmpXhID ="-1";
                    //数据集
                    DataSet ds = new DataSet();
                    //主表
                    DataTable dt = new DataTable();
                    //装箱日期
                    string strPackageDate = sqt.GetServerDate();
                    strPackageDate = strPackageDate.Substring(0, 4) + "年" + strPackageDate.Substring(4, 2) + "月" + strPackageDate.Substring(6, 2) + "日";
                    int iCount = 1;
                    for (int i = 0; i < tmpdt.Rows.Count; i++)
                    {
                        if (!tmpdt.Rows[i]["销货单号"].Equals(strTmpXhID))
                        {
                            //当前行销货单号与缓存销货单号不一致时，保存当前缓存数据，更新销货单号
                            if (dt.Rows.Count > 0)
                            {
                                lstDs.Add(ds);
                            }
                            //声明新数据表
                            ds = new DataSet();
                            dt = new DataTable();
                            ds.Tables.Add(dt);
                            dt.TableName = "ReportData";
                            dt.Columns.Add("送货日期");
                            dt.Columns.Add("序号");
                            dt.Columns.Add("合同号");
                            dt.Columns.Add("规格型号");
                            dt.Columns.Add("单位");
                            dt.Columns.Add("数量");
                            dt.Columns.Add("产品名称");
                            dt.Columns.Add("送货单位主管");
                            dt.Columns.Add("送货单位库房保管");
                            dt.Columns.Add("送货单位经办");
                            //缓存销货单号
                            strTmpXhID = GeneralTools.GetString(tmpdt.Rows[i]["销货单号"], string.Empty);
                            //初始化行标
                            iCount = 1;
                        }
                        //将当前行添加到数据表
                        DataRow tmpdr = dt.NewRow();
                        tmpdr["送货日期"] = strPackageDate;
                        tmpdr["序号"] = iCount;
                        tmpdr["合同号"] = tmpdt.Rows[i]["合同号"];
                        tmpdr["规格型号"] = tmpdt.Rows[i]["规格型号"];
                        tmpdr["单位"] = "只";
                        tmpdr["数量"] = tmpdt.Rows[i]["数量"];
                        tmpdr["产品名称"] = "电容器";
                        tmpdr["送货单位主管"] = strManager;
                        tmpdr["送货单位库房保管"] = strStorageManager;
                        tmpdr["送货单位经办"] = strOperator;
                        if (!(lstXhdh.IndexOf(GeneralTools.GetString(tmpdt.Rows[i]["销货单号"], string.Empty))>=0))
                            lstXhdh.Add(GeneralTools.GetString(tmpdt.Rows[i]["销货单号"], string.Empty));
                        dt.Rows.Add(tmpdr);
                        iCount++;
                        if (i == tmpdt.Rows.Count - 1)
                        {
                            //如果已读取到最后一行，则保存数据表
                            lstDs.Add(ds);
                        }
                    }
                    ReportDocument tmprd = new ReportDocument();
                    tmpsb = new StringBuilder();
                    GeneralTools.GetPrivateProfileString("Config", "Printer", string.Empty, tmpsb, 256, System.Windows.Forms.Application.StartupPath + "\\Plugin\\Print_Zxysd_ch_Config.ini");
                    string strTmpPrinterName = tmpsb.ToString();
                    GeneralTools.GetPrivateProfileString("Config", "CopyCount", "1", tmpsb, 256, System.Windows.Forms.Application.StartupPath + "\\Plugin\\Print_Zxysd_ch_Config.ini");
                    int iCopyCount = GeneralTools.GetInt(tmpsb.ToString(), 1);
                    tmprd.Load(System.Windows.Forms.Application.StartupPath + "\\Plugin\\Report_ch.rpt");
                    //打印
                    for (int i = 0; i < lstDs.Count; i++)
                    {
                        tmprd.SetDataSource(lstDs[i].Tables["ReportData"]);
                        tmprd.PrintOptions.PrinterName = strTmpPrinterName;
                        tmprd.PrintToPrinter(iCopyCount, false, 0, 0);
                    }
                    //更新销货单打印记录
                    for(int i=0;i<lstXhdh.Count;i++)
                    {
                        sqt.DoCmdExecuteNonQuery("insert into zxysd_ch (销货单号) values ('" + lstXhdh[i] + "')");
                    }
                }
                catch (Exception ex)
                {
                    GeneralTools.ShowErrorMsg("出现错误：" + ex.Message);
                }
            }
        }
    }
}
